package skuorder

import (
	"encoding/json"
	"strconv"
	"oxy_server/model"
	"oxy_server/module/log"
	"oxy_server/until"
	"time"

	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
)

type OXYSkuOrderV3 struct{}

func NewOXYSkuOrderV3() *OXYSkuOrderV3 {
	Data := &OXYSkuOrderV3{}
	return Data
}

func (v3 *OXYSkuOrderV3) GetSkuOrderListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	sort := c.QueryParam("sort")
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "sku_order_v3"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	query, count, err := model.DB().QuerySort(M, currentpage, perpage, sort)
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []Skuorder{}
	query.All(&ps)

	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  string(str),
		"count": count,
	})

	return nil
}

// 订单模式下生产组创建的 订单
func (v3 *OXYSkuOrderV3) AddSkuOrderHandler(c echo.Context) error {
	log.Info("AddSkuOrderHandlerNew")
	s := &insertskuorder{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "sku_order_v3"
	err := model.DB().Insert(s)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	skuorderList := []Skuorder{}
	queryorder, _, err := model.DB().Query(bson.M{"productid": s.ProductID}, 0, 0)
	if err != nil {
		log.Error("计算产品下单数入库数出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	queryorder.All(&skuorderList)

	OrderCount := 0
	Count := 0
	for _, order := range skuorderList {
		orderCount, _ := strconv.Atoi(order.OrderCount)
		count, _ := strconv.Atoi(order.Count)
		OrderCount += orderCount
		Count += count
	}
	// 新建订单时更新生产组的入库时间
	InboundDate := until.FormatTime(s.Time)
	h, _ := time.ParseDuration("1h")
	InboundDate = InboundDate.Add(8 * h)
	model.DB().Document = "product_plan_order"
	selector := bson.M{"_id": bson.ObjectIdHex(s.ProductID)}
	data := bson.M{"$set": bson.M{"inbounddate": InboundDate, "ordercount": OrderCount, "count": Count}}
	err = model.DB().Update(selector, data)
	if err != nil {
		log.Error("更新出错，生产组最新入库时间", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}

// 订单模式下产品运迎创建的 订单
func (v3 *OXYSkuOrderV3) UpdateSkuOrderHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateSkuOrderHandler", id)
	s := &insertskuorder{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "sku_order_v3"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, s)
	if err != nil {
		log.Error("更新出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	skuorderList := []Skuorder{}
	queryorder, _, err := model.DB().Query(bson.M{"productid": s.ProductID}, 0, 0)
	if err != nil {
		log.Error("计算产品下单数入库数出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	queryorder.All(&skuorderList)
	OrderCount := 0
	Count := 0
	for _, order := range skuorderList {
		orderCount, _ := strconv.Atoi(order.OrderCount)
		count, _ := strconv.Atoi(order.Count)
		OrderCount += orderCount
		Count += count
	}
	// 更新订单时更新生产组的入库时间
	InboundDate := until.FormatTime(s.Time)
	h, _ := time.ParseDuration("1h")
	InboundDate = InboundDate.Add(8 * h)
	model.DB().Document = "product_plan_order"
	selector := bson.M{"_id": bson.ObjectIdHex(s.ProductID)}
	data := bson.M{"$set": bson.M{"inbounddate": InboundDate, "ordercount": OrderCount, "count": Count}}
	err = model.DB().Update(selector, data)
	if err != nil {
		log.Error("更新出错，生产组最新入库时间", err)
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}

func (v3 *OXYSkuOrderV3) DelSkuOrderHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelSkuOrderHandler", id)
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "sku_order_v3"
	s := insertskuorder{}
	query, _ := model.DB().QueryOne(bson.M{"_id": bson.ObjectIdHex(id)})
	query.One(&s)

	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	skuorderList := []Skuorder{}
	queryorder, _, err := model.DB().Query(bson.M{"productid": s.ProductID}, 0, 0)
	if err != nil {
		log.Error("计算产品下单数入库数出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	queryorder.All(&skuorderList)
	OrderCount := 0
	Count := 0
	for _, order := range skuorderList {
		orderCount, _ := strconv.Atoi(order.OrderCount)
		count, _ := strconv.Atoi(order.Count)
		OrderCount += orderCount
		Count += count
	}
	// 更新订单时更新生产组的入库时间
	InboundDate := until.FormatTime(s.Time)
	h, _ := time.ParseDuration("1h")
	InboundDate = InboundDate.Add(8 * h)
	model.DB().Document = "product_plan_order"
	selector := bson.M{"_id": bson.ObjectIdHex(s.ProductID)}
	data := bson.M{"$set": bson.M{"inbounddate": InboundDate, "ordercount": OrderCount, "count": Count}}
	err = model.DB().Update(selector, data)
	if err != nil {
		log.Error("更新出错v3，生产组最新入库时间", err)
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})
	return nil
}
func (v1 *OXYSkuOrderV3) UpdateSkuOrderInfo(c echo.Context) error {
	return nil
}
